#include "widget.h"
#include "ui_widget.h"
#include <QByteArray>
#include <QDebug>
#include <QMessageBox>
#include <QStringList>
#include "protocol.h"
#include "InnerWorkComponent/findwidget.h"
#include "Core/tcpclient.h"
#include "WorkServe/cuserservice.h"
#include "Core/messagedispatcher.h"

Widget *Widget::UserUpSet=nullptr;
QMutex Widget::UserUpSetMutex;
Widget *Widget::get_instance()
{
    if(UserUpSet==nullptr){
        QMutexLocker locker(&UserUpSetMutex);
        if(UserUpSet==nullptr){
            UserUpSet=new Widget;
            qAddPostRoutine(destroy_instance);
        }
    }
    return UserUpSet;
}


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    UserAction=nullptr;
    tcpclient=TcpClient::get_instance();
    LinkTcpSignalAndSlot();
    tcpclient->Do_Connect();
}

Widget::~Widget()
{
    delete ui;  
}
void Widget::TcpConnectStatus()
{
    QMessageBox::information(this,"连接服务器","成功");
}

void Widget::TcpConnectFailStatus(QString cause)
{
    QMessageBox::information(this,"连接服务器","失败："+cause);
}

void Widget::ActForRegisterFail(QString info)
{
    QMessageBox::information(this,"注册","失败："+info);
}

void Widget::ActForRegisterSuccess(QString info)
{
    QMessageBox::information(this,"注册","成功："+info);
}

void Widget::ActForLoginFail(QString info)
{
    QMessageBox::information(this,"登录","失败："+info);
}

void Widget::ActForLoginSuccess(QString info)
{
    qDebug()<<__FILE__<<__LINE__<<info;
    EnterUserAction();
}
void Widget::on_LoginBtn_clicked()
{
    ui->UserStack->setCurrentIndex(0);
}

void Widget::on_DeleteBtn_clicked()
{
    ui->UserStack->setCurrentIndex(1);
}

void Widget::on_RegisterBtn_clicked()
{
    ui->UserStack->setCurrentIndex(2);
}

void Widget::on___register_clicked()
{
    QString usrName=ui->RUserInput->text();
    QString usrPwd=ui->RPwdInput->text();
    QString R_pwd=ui->RRPwdInput->text();
    if(!usrName.length()||!usrPwd.length()){
        QMessageBox::information(this,"注册","不能为空");
        return;
    }
    if(usrPwd!=R_pwd){
        QMessageBox::information(this,"注册","前后密码不匹配");
        return;
    }
    QJsonObject Info;
    Info["USER"]=usrName;
    Info["PWD"]=usrPwd;
    emit sig_register(MSG_TYPE_USER_REGISER_REQUEST,Info);
}

void Widget::on___login_clicked()
{
    QString usrName=ui->LUserInput->text();
    QString usrPwd=ui->LPwdInput->text();
    if(!usrName.length()||!usrPwd.length()){
        QMessageBox::warning(this,"登录","信息不能为空");
        return;
    }
    QJsonObject Info;
    Info["USER"]=usrName;
    Info["PWD"]=usrPwd;
    emit sig_Login(MSG_TYPE_USER_LOGIN_REQUEST,Info);
}

void Widget::EnterUserAction()
{
    if(UserAction!=nullptr){
        delete UserAction;
        UserAction=nullptr;
    }
    UserAction=new ActionWidget;
    UserAction->setUserName(TcpClient::get_instance()->getUserName());
    UserAction->show();
    this->close();
}

void Widget::destroy_instance()
{
    //qDebug()<<__FILE__<<__FUNCTION__<<__LINE__;
    if(UserUpSet==nullptr)return;
    delete UserUpSet;
    UserUpSet=nullptr;
}

void Widget::LinkTcpSignalAndSlot()
{

    connect(tcpclient,&TcpClient::sigConnectSuccess,
            this,&Widget::TcpConnectStatus);
    connect(tcpclient,&TcpClient::sigConnectFail,
            this,&Widget::TcpConnectFailStatus);


    connect(MessageDispatcher::GetInstance()->getCuserServer(),
            &CUserService::EmitRegisterFail,
            this,&Widget::ActForRegisterFail);
    connect(MessageDispatcher::GetInstance()->getCuserServer(),
            &CUserService::EmitRegisterSuccess,
            this,&Widget::ActForRegisterSuccess);
    connect(MessageDispatcher::GetInstance()->getCuserServer(),
            &CUserService::EmitLoginFail,
            this,&Widget::ActForLoginFail);
    connect(MessageDispatcher::GetInstance()->getCuserServer(),
            &CUserService::EmitLoginSuccess,
            this,&Widget::ActForLoginSuccess);


    connect(this,&Widget::sig_Login,
            tcpclient,&TcpClient::SendMsg);

    connect(this,&Widget::sig_register,
            tcpclient,&TcpClient::SendMsg);
}

